package com.sanjiahantang.youjiarole.module.evaluate.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.evaluate.beans.EvaGoodsBean;
import com.sanjiahantang.youjiarole.module.evaluate.beans.EvaServiceBean;
import com.sanjiahantang.youjiarole.module.evaluate.beans.Image;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

/**
 * Created by zy on 2017/9/6.
 */

public class MeEvaluateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_SERVICE = 0x0001;
    public static final int TYPE_GOODS = 0x0002;

    private Context context;
    private Integer evaType;
    private LayoutInflater inflater;
    private EvaServiceBean serviceData;
    private EvaGoodsBean goodsData;
    private HashMap<Integer,String> roleTypeMap;
    public MeEvaluateAdapter(Context context, Integer evaType){
        this.context = context;
        this.evaType = evaType;
        this.inflater = LayoutInflater.from(context);
    }

    public MeEvaluateAdapter(Context context, Integer evaType, EvaServiceBean data){
        this(context,evaType);
        serviceData = data;
        roleTypeMap = new HashMap<>();
        roleTypeMap.put(0,"设计师");
        roleTypeMap.put(1,"项目经理");
        roleTypeMap.put(2,"监理");
        roleTypeMap.put(3,"体验店");
    }

    public MeEvaluateAdapter(Context context, Integer evaType, EvaGoodsBean data){
        this(context,evaType);
        goodsData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (evaType){
            case TYPE_SERVICE:
                return new EvaHolder(inflater.inflate(R.layout.item_eva_service,null),evaType,context,serviceData.getData().getList().get(viewType));
            case TYPE_GOODS:
                return new EvaHolder(inflater.inflate(R.layout.item_eva_goods,null),evaType,context,goodsData.getData().getList().get(viewType));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (evaType == TYPE_GOODS){
            return goodsData.getData().getList().size();
        }else {
            return serviceData.getData().getList().size();
        }

    }

    private class EvaHolder extends RecyclerView.ViewHolder{

        public EvaHolder(View itemView) {
            super(itemView);
        }

        public EvaHolder(View itemView, int type, Context context, EvaServiceBean.Item data){
            this(itemView);
            TextView txtTargetJob = (TextView) itemView.findViewById(R.id.txtTargetJob);
            txtTargetJob.setText(data.getRoleTypeName());

            TextView txtTargetName = (TextView) itemView.findViewById(R.id.txtTargetName);
            txtTargetName.setText(data.getRoleName());

            TextView txtEvaluate = (TextView) itemView.findViewById(R.id.txtEvaluate);
            txtEvaluate.setText(data.getPlace());

            ImageView ivTargetPhoto = (ImageView) itemView.findViewById(R.id.ivTargetPhoto);
            Picasso.with(context).load(FileUtils.rePath(data.getUserUrl())).into(ivTargetPhoto);

            RatingBar ratingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
            ratingBar.setRating(data.getUserNumber());
            ratingBar.setIsIndicator(true);

            MyGridView gridView = (MyGridView) itemView.findViewById(R.id.gridView);
            gridView.setAdapter(new QuickAdapter<Image>(context,R.layout.item_image,data.getImages()) {
                @Override
                protected void convert(BaseAdapterHelper helper, final Image item) {
                    ImageView iv = helper.getView(R.id.image);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into(iv);
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Utils.showPicDialog((Activity) context,FileUtils.rePath(item.getUrl()),R.drawable.i_rect_bg);
                        }
                    });
                }
            });

        }

        public EvaHolder(View itemView, int type, Context context, EvaGoodsBean.Item data){
            this(itemView);
            TextView txtContent = (TextView) itemView.findViewById(R.id.txtContent);
            txtContent.setText(data.getPlace());


            TextView txtFeedBack = (TextView) itemView.findViewById(R.id.txtFeedBack);
            if (data.getReplyPlace() != null){
                txtFeedBack.setText("回复：" + data.getReplyPlace());
                itemView.findViewById(R.id.replyLayout).setVisibility(View.VISIBLE);
            }else{
                itemView.findViewById(R.id.replyLayout).setVisibility(View.GONE);
            }

            TextView txtCreateTime = (TextView) itemView.findViewById(R.id.txtCreateTime);
            txtCreateTime.setText(data.getCreateTime().substring(0,"yyyy-MM-hh".length()));

            if (data.getNumber() != null){
                RatingBar ratingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
                try {
                    ratingBar.setRating(Float.parseFloat(data.getNumber()));
                    ratingBar.setIsIndicator(true);
                }catch (NumberFormatException e){
                    Log.e("rating",e.getMessage());
                }

            }

            if (data.getGoodsPropertyName() != null){
                TagGroup tagGroup = (TagGroup) itemView.findViewById(R.id.tagGroup);
                tagGroup.setTags(data.getGoodsPropertyName().split(","));
            }

            MyGridView gvImageList = (MyGridView) itemView.findViewById(R.id.gvImageList);
            gvImageList.setAdapter(new QuickAdapter<Image>(context,R.layout.item_image,data.getImages()) {
                @Override
                protected void convert(BaseAdapterHelper helper, final Image item) {
                    ImageView iv = helper.getView(R.id.image);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into(iv);
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Utils.showPicDialog((Activity) context,FileUtils.rePath(item.getUrl()),R.drawable.i_rect_bg);
                        }
                    });
                }
            });

            TextView txtGoodsName = (TextView) itemView.findViewById(R.id.txtGoodsName);
            txtGoodsName.setText(data.getGoodsName());

            TextView txtGoodsPrice = (TextView) itemView.findViewById(R.id.txtGoodsPrice);
            txtGoodsPrice.setText(String.format("¥ %.2f" ,data.getBasePrice()));

            ImageView ivGoodsImage = (ImageView) itemView.findViewById(R.id.ivGoodsImage);
            Picasso.with(context).load(FileUtils.rePath(data.getMallUrl())).into(ivGoodsImage);
        }
    }

}
